<?php
//by willson 105328468@qq.com 2014/10/11
//index page interface

class goods extends core {

	public function __construct() {
		parent::__construct();
	}

	//列表
	public function index() {
		$where = '';
		$id = (isset($_GET['id']) && trim($_GET['id'])) ? intval($_GET['id']) : '';
        $keyword = (isset($_GET['keyword']) && trim($_GET['keyword'])) ? trim($_GET['keyword']) : '';
        $shop_id = (isset($_GET['shop_id']) && trim($_GET['shop_id'])) ? trim($_GET['shop_id']) : '';
        $huohao = (isset($_GET['huohao']) && trim($_GET['huohao'])) ? trim($_GET['huohao']) : '';
        $cate_id = (isset($_GET['cate_id']) && trim($_GET['cate_id'])) ? intval($_GET['cate_id']) : 0;
        $startprice = (isset($_GET['startprice']) && trim($_GET['startprice'])) ? trim($_GET['startprice']) : '';
        $endprice = (isset($_GET['endprice']) && trim($_GET['endprice'])) ? trim($_GET['endprice']) : '';
        $jiage = (isset($_GET['jiage']) && trim($_GET['jiage'])) ? intval($_GET['jiage']) : 0;
        $oldwhere = (isset($_GET['oldwhere']) && trim($_GET['oldwhere'])) ? trim($_GET['oldwhere']) : '';
        // $cate_boss = (isset($_GET['cate_boss']) && trim($_GET['cate_boss'])) ? intval($_GET['cate_boss']) : 0;

        if ($keyword) {
            $where .= "AND (g.name LIKE '%{$keyword}%' OR g.huohao LIKE '%{$keyword}%' 
            	OR g.description LIKE '%{$keyword}%' OR g.content LIKE '%{$keyword}%')";
        }
        if ($id) {
            $where .= "AND g.id='{$id}'";
        }
        if ($shop_id) {
			$where .= " AND g.shop_id='{$shop_id}'";
		}
		if ($huohao) {
			$where .= " AND g.huohao='{$huohao}'";
		}
		if ($startprice >= 0) {
			$where .= " AND (g.price >= '{$startprice}')";
		}
		if($jiage > 0){
			if ($endprice >= 0) {
			$where .= "AND (g.price <= '{$endprice}')";
			}
		}
		if ($cate_id) {
			$where .= " AND g.category_id = '{$cate_id}'";
		}
		$oldwhere = urlencode("keyword={$keyword}&shop_id={$shop_id}&startprice={$startprice}&endprice={$endprice}&cate_id={$cate_id}");
        //分页
        $this->setpages(array('id'=>$id, 'keyword'=>$keyword ,'shop_id'=>$shop_id,'huohao'=>$huohao,
        	'startprice'=>$startprice,'endprice'=>$endprice,'cate_id'=>$cate_id));

		// $this->ezr->num_results_per_page = 100;
		$sql = "SELECT g.* ,c.name as cname ,b.name as bname ,s.name as sname
				 FROM jf_goods g 
		 		 LEFT JOIN jf_goods_category c ON g.category_id =c.id 
		 		 LEFT JOIN jf_brand b ON g.brand_id = b.id 
		 		 LEFT JOIN jf_shop s ON g.shop_id = s.id 
		 		 WHERE 1 {$where} ORDER BY g.id DESC";
		$rs = $this->ezr->get_results($sql);
		$nav = $this->ezr->get_navigation();

		$sql = "SELECT id,name FROM jf_shop ORDER BY  id ASC";
		$shops = $this->db->get_results($sql);
		$sql = "SELECT name ,id ,parent_id FROM jf_goods_category WHERE 1";
		$cate = $this->db->get_results($sql);
		$sql = "SELECT name ,id ,parent_id FROM jf_goods_category WHERE parent_id = 0";
		$cate_parent = $this->db->get_results($sql);
		$this->smarty->assign('shops',$shops);
		$this->smarty->assign('rs',$rs);
		$this->smarty->assign('page',$nav);
		$this->smarty->assign('shop_id',$shop_id);
		$this->smarty->assign('startprice',$startprice);
		$this->smarty->assign('endprice',$endprice);
		$this->smarty->assign('cate_id',$cate_id);
		$this->smarty->assign('cate',$cate);
		$this->smarty->assign('cate_parent',$cate_parent);
		$this->smarty->assign('oldwhere',$oldwhere);
		// $this->smarty->assign('cate_boss',$cate_boss);
		$this->display();
	}
	//add
	public function add() {
		if (IS_POST) {
			$this->save();
		} else {
			$sql = "SELECT name ,id FROM jf_shop WHERE 1";
			$shop = $this->db->get_results($sql);
			$sql = "SELECT name ,id FROM jf_country WHERE 1";
			$country = $this->db->get_results($sql);
			$sql = "SELECT name ,id ,parent_id FROM jf_goods_category WHERE 1";
			$cate = $this->db->get_results($sql);
			$sql = "SELECT name ,id ,parent_id FROM jf_goods_category WHERE parent_id = 0";
			$cate_parent = $this->db->get_results($sql);
			//品牌
			$sql = "SELECT name ,id FROM jf_brand WHERE status = 1";
			$brand = $this->db->get_results($sql);


			$this->smarty->assign('shop',$shop);
			$this->smarty->assign('country',$country);
			$this->smarty->assign('brand',$brand);
			$this->smarty->assign('cate',$cate);
			$this->smarty->assign('cate_parent',$cate_parent);
			$this->display();
		}
	}	

	//编辑
	public function edit() {
		if (IS_POST) {
			$this->save();
		} else {
			//店铺
			$sql = "SELECT name ,id FROM jf_shop WHERE 1";
			$shop = $this->db->get_results($sql);
			//国家馆
			$sql = "SELECT name ,id FROM jf_country WHERE 1";
			$country = $this->db->get_results($sql);
			//品牌
			$sql = "SELECT name ,id FROM jf_brand WHERE status = 1";
			$brand = $this->db->get_results($sql);
			//分类
			$sql = "SELECT name ,id ,parent_id FROM jf_goods_category WHERE 1";
			$cate = $this->db->get_results($sql);
			$sql = "SELECT name ,id ,parent_id FROM jf_goods_category WHERE parent_id = 0";
			$cate_parent = $this->db->get_results($sql);
			//商品'
			$goods_id = (isset($_GET['goods_id']) && intval($_GET['goods_id'])) ? intval($_GET['goods_id']) : 0;
			$sql = "SELECT * FROM jf_goods WHERE id='$goods_id' ";
			$goods = $this->db->get_row($sql);
			//商品图
			$sql = "SELECT * FROM jf_goods_pic WHERE goods_id = '{$goods_id}'";
			$pics = $this->db->get_results($sql);
			//规格
			$sql = "SELECT * FROM jf_goods_spec WHERE goods_id = '{$goods_id}'";
			$spec = $this->db->get_results($sql);//var_dump($spec);exit;
			$oldwhere = (isset($_GET['oldwhere']) && trim($_GET['oldwhere'])) ? trim($_GET['oldwhere']) : '';
			//在列表页第几页进入编辑
			$current_page = (isset($_GET['current_page']) && trim($_GET['current_page'])) ? intval($_GET['current_page']) : 0;
			// urlencode($oldwhere);
			// var_dump($oldwhere);exit;
			$this->smarty->assign('spec', $spec);
			$this->smarty->assign('pics', $pics);
			$this->smarty->assign('shop',$shop);
			$this->smarty->assign('country',$country);
			$this->smarty->assign('brand',$brand);
			$this->smarty->assign('cate',$cate);
			$this->smarty->assign('cate_parent',$cate_parent);
			$this->smarty->assign('goods', $goods);
			$this->smarty->assign('oldwhere', $oldwhere);
			$this->smarty->assign('current_page', $current_page);
			$this->display();
		}
	}
	public function upload_pic(){
        global $domain;
        $file_result = upload_one_file('article','pic_file',true);
        $file_result = $file_result['file'];
        echo $file_result;
    }
	

	//保存
	public function save() {
		$goods_id = (isset($_POST['goods_id']) && intval($_POST['goods_id'])) ? intval($_POST['goods_id']) : 0;
		$name = (isset($_POST['name']) && trim($_POST['name'])) ? trim($_POST['name']) : '';
		$huohao = (isset($_POST['huohao']) && trim($_POST['huohao'])) ? trim($_POST['huohao']) : '';
		$description = (isset($_POST['description']) && trim($_POST['description'])) ? trim($_POST['description']) : '';
		$keywords = (isset($_POST['keywords']) && trim($_POST['keywords'])) ? trim($_POST['keywords']) : '';
		$content = (isset($_POST['content']) && trim($_POST['content'])) ? trim($_POST['content']) : '';
		$weight = (isset($_POST['weight']) && trim($_POST['weight'])) ? trim($_POST['weight']) : '';
		$size = (isset($_POST['size']) && trim($_POST['size'])) ? trim($_POST['size']) : '';
		$price = (isset($_POST['price']) && trim($_POST['price'])) ? trim($_POST['price']) : '';
		$market_price = (isset($_POST['market_price']) && trim($_POST['market_price'])) ? trim($_POST['market_price']) : '';
		$cost_price = (isset($_POST['cost_price']) && trim($_POST['cost_price'])) ? trim($_POST['cost_price']) : '';
		$promote_price = (isset($_POST['price_count']) && trim($_POST['price_count'])) ? trim($_POST['price_count']) : '';
		$promote_begin_time = (isset($_POST['begin_time']) && trim($_POST['begin_time'])) ? trim($_POST['begin_time']) : '';
		$promote_end_time = (isset($_POST['end_time']) && trim($_POST['end_time'])) ? trim($_POST['end_time']) : '';
		$integral = (isset($_POST['integral']) && intval($_POST['integral'])) ? intval($_POST['integral']) : 0;
		$give_integral = (isset($_POST['give_integral']) && intval($_POST['give_integral'])) ? intval($_POST['give_integral']) : 0;
		$category_id = (isset($_POST['cate']) && intval($_POST['cate'])) ? intval($_POST['cate']) : 0;
		$shop_id = (isset($_POST['shop']) && intval($_POST['shop'])) ? intval($_POST['shop']) : 0;
		$brand_id = (isset($_POST['brand']) && intval($_POST['brand'])) ? intval($_POST['brand']) : 0;
		$country_id = (isset($_POST['country']) && intval($_POST['country'])) ? intval($_POST['country']) : 0;
		$stocks = (isset($_POST['stocks']) && intval($_POST['stocks'])) ? intval($_POST['stocks']) : 0;
		$stock_alert_number = (isset($_POST['stocks_dg']) && intval($_POST['stocks_dg'])) ? intval($_POST['stocks_dg']) : 0;
		$ext_property = isset($_POST['ext_property']) ? $_POST['ext_property'] : '';
		$shipping_fee = (isset($_POST['fee']) && trim($_POST['fee'])) ? trim($_POST['fee']) : '';
		$free_shipping = (isset($_POST['free']) && intval($_POST['free'])) ? intval($_POST['free']) : 0;
		$commission_template_id = (isset($_POST['com_id']) && intval($_POST['com_id'])) ? intval($_POST['com_id']) : 0;
		$commissions = (isset($_POST['com']) && trim($_POST['com'])) ? trim($_POST['com']) : '';
		$sort = (isset($_POST['sort']) && intval($_POST['sort'])) ? intval($_POST['sort']) : 0;
		$status = (isset($_POST['status']) && intval($_POST['status'])) ? intval($_POST['status']) : 0;
		$seller_note = (isset($_POST['note']) && trim($_POST['note'])) ? trim($_POST['note']) : '';
		$in_shop = (isset($_POST['in_shop']) && intval($_POST['in_shop'])) ? intval($_POST['in_shop']) : 0;
		$sale_method = (isset($_POST['sale_method']) && intval($_POST['sale_method'])) ? intval($_POST['sale_method']) : 0;
		$spec = (isset($_POST['spec']) && intval($_POST['spec'])) ? $_POST['spec'] : '' ;
		$pics = (isset($_POST['pics']) && intval($_POST['pics'])) ? $_POST['pics']: '';
		$sort_pic = (isset($_POST['sort_pic']) && intval($_POST['sort_pic'])) ? $_POST['sort_pic'] : '';
		$sto = (isset($_POST['sto']) && intval($_POST['sto'])) ? $_POST['sto'] : '';
		$pri = (isset($_POST['pri']) && intval($_POST['pri'])) ? $_POST['pri'] : '';
		$pro = (isset($_POST['pro']) && intval($_POST['pro'])) ? $_POST['pro'] : '';
		$oldwhere = (isset($_POST['oldwhere']) && trim($_POST['oldwhere'])) ? trim($_POST['oldwhere']) : '';
		$BRSR = (isset($_POST['current_page']) && trim($_POST['current_page'])) ? trim($_POST['current_page']) : '';
		if ($promote_begin_time) $promote_begin_time=strtotime($promote_begin_time);
		if ($promote_end_time) $promote_end_time=strtotime($promote_end_time);
		//图片
		$pic = upload_one_file('default_pic', 'default_pic');
		$memo = upload_one_file('memo', 'memo');
		if($free_shipping == 1){
			$shipping_fee = '';
		}
		if($ext_property){
			if (is_array($ext_property)) $ext_property = implode(',', $ext_property);
		}
		if ($goods_id) {
			$sql = "UPDATE jf_goods SET name='{$name}', huohao='{$huohao}',description='{$description}',keywords='{$keywords}',content='{$content}',
					weight='{$weight}',size='{$size}',price='{$price}',market_price='{$market_price}',cost_price='{$cost_price}',promote_price='{$promote_price}',
					promote_begin_time='{$promote_begin_time}',promote_end_time='{$promote_end_time}',integral='{$integral}',
					give_integral='{$give_integral}',category_id='{$category_id}',shop_id='{$shop_id}',brand_id='{$brand_id}',
					country_id='{$country_id}',stocks='{$stocks}',stock_alert_number='{$stock_alert_number}',ext_property='{$ext_property}',
					shipping_fee='{$shipping_fee}',free_shipping='{$free_shipping}',commission_template_id='{$commission_template_id}',
					commissions='{$commissions}',sort='{$sort}',status='{$status}',seller_note='{$seller_note}',in_shop='{$in_shop}',
					sale_method='{$sale_method}',edit_time='".time()."' 
					WHERE id='{$goods_id}' ";
			if($this->db->query($sql) || $memo){
				//更新商品表图片
				if($pic){
					$sql = "UPDATE jf_goods SET default_pic='{$pic}'WHERE id ='{$goods_id}' ";
					$this->db->query($sql);
					// $sql = "UPDATE jf_goods_pic SET pic='{$pic}' WHERE goods_id ='{$goods_id}' ";
					// $this->db->query($sql);
				}
				if($memo){
					$sql = "UPDATE jf_goods_pic SET memo='{$memo}'  WHERE goods_id ='{$goods_id}' ";
					$this->db->query($sql);
				}
				if($keywords){
					$sql = "UPDATE jf_goods_keyword SET keyword='{$keywords}' WHERE goods_id ='{$goods_id}' ";
					$this->db->query($sql);
				}
				//
				//更新产品规格表
				$sql="DELETE FROM jf_goods_spec WHERE goods_id='{$goods_id}'";
                $this->db->query($sql);
				if($goods_id && $spec){
                    $sql="INSERT INTO jf_goods_spec(goods_id,spec,stocks,price,promote_price) VALUES ";
                    foreach($spec as $k => $p){
                     	foreach ($sto as $kk => $stocks) {
                     		if($k == $kk){
								foreach ($pri as $kkk=> $price) {
									if($kkk == $kk){
										if($pro){
											foreach ($pro as $ka => $promote_price) {
												if($ka == $kkk){
													$sql.=" ('{$goods_id}','{$p}','{$stocks}','{$price}','{$promote_price}') ,";
												}
											}
										}else{
											$sql.=" ('{$goods_id}','{$p}','{$stocks}','".intval($price)."','0') ,";
										}
									}
								}
							}
						}
                    }
                    $sql=substr($sql,0,strlen($sql)-1);
                    $this->db->query($sql);
                }
				//更新商品图片表
				if($pics || $sort_pic){
                    $sql="DELETE FROM jf_goods_pic WHERE goods_id='{$goods_id}'";
                    $this->db->query($sql);
                   	$sql="INSERT INTO jf_goods_pic(goods_id,pic,add_time,sort ,memo) VALUES ";
                    foreach($pics as $key => $pic){
                    	foreach ($sort_pic as $ks =>  $sort) {
                    		if($key == $ks){
                    			$sql.="('{$goods_id}','{$pic}','".time()."','{$sort}','{$memo}') ,";
                    		}
                    	}
                    }
                    $sql=substr($sql,0,strlen($sql)-1);
                    $this->db->query($sql);
                }
				echo"<meta charset='UTF-8'><script>alert('修改成功');window.location.href='?app=goods&act=index&{$oldwhere}&BRSR={$BRSR}'</script>";
			}else{
				echo"<meta charset='UTF-8'><script>alert('没有修改');window.location.href='?app=goods&act=index&{$oldwhere}&BRSR={$BRSR}'</script>";
			}
			exit;
		} else {//add
			$sql = "INSERT INTO jf_goods(name, huohao,description ,keywords,content,weight,size,price,market_price,cost_price,promote_price,
							promote_begin_time,promote_end_time,integral,give_integral,category_id,shop_id,brand_id,country_id,
							stocks,stock_alert_number,ext_property,shipping_fee,free_shipping,commission_template_id,commissions,
							sort,status,seller_note,in_shop,sale_method,add_time,default_pic) 
						VALUES(
							'{$name}', '{$huohao}', '{$description}', '{$keywords}', '{$content}', '{$weight}','{$size}','{$price}',
							'{$market_price}','{$cost_price}', '{$promote_price}', '{$promote_begin_time}' , '{$promote_end_time}', 
							'{$integral}','{$give_integral}','{$category_id}','{$shop_id}','{$brand_id}','{$country_id}',
							'{$stocks}','{$stock_alert_number}','{$ext_property}','{$shipping_fee}','{$free_shipping}',
							'{$commission_template_id}','{$commissions}','{$sort}','{$status}','{$seller_note}','{$in_shop}',
							'{$sale_method}','".time()."','{$pic}')";
			$this->db->query($sql);
			$goods_id = $this->db->insert_id;
			if($goods_id){
				//如果添加新产品成功，就把该关键词插入产品关键词表
				$sql = "INSERT INTO jf_goods_keyword (keyword , goods_id) VALUES ('{$keywords}','{$goods_id}')";
				$this->db->query($sql);
				//如果添加新产品成功，就把该规格信息插入产品规格表
				if($goods_id && $spec){
                    $sql="INSERT INTO jf_goods_spec(goods_id,spec,stocks,price,promote_price) VALUES ";
                    foreach($spec as $k => $p){
                     	foreach ($sto as $kk => $stocks) {
                     		if($k == $kk){
								foreach ($pri as $kkk=> $price) {
									if($kkk == $kk){
										foreach ($pro as $ka => $promote_price) {
											if($ka == $kkk){
												$sql.=" ('{$goods_id}','{$p}','{$stocks}','{$price}','{$promote_price}') ,";
											}
										}
									}
								}
                     		}
                     	}	
					}
					$sql=substr($sql,0,strlen($sql)-1);
					$this->db->query($sql);
                }
				//如果添加新产品成功，就把该图片插入产品照片表
				if($goods_id && $pics){
                    $sql="INSERT INTO jf_goods_pic(goods_id,pic,add_time,sort,memo) VALUES ";
                     foreach($pics as $key => $pic){
                    	foreach ($sort_pic as $ks =>  $sort) {
                    		if($key == $ks){
                    			$sql.=" ('{$goods_id}','{$pic}','".time()."','{$sort}','{$memo}') ,";
                    		}
                    	}
                    }
                    $sql=substr($sql,0,strlen($sql)-1);
                    $this->db->query($sql);
                }
				echo"<meta charset='UTF-8'><script>alert('添加成功');window.location.href='?app=goods&act=index';</script>";
			}else{
				echo"<meta charset='UTF-8'><script>alert('添加失败');window.location.href='?app=goods&act=index';</script>";
			}
			exit;
		}
		urldecode($oldwhere);
		if($BRSR > 1){
			$BRSR = ($BRSR - 1)*10;
		}
		header("Location:?app=goods&act=index&{$oldwhere}&BRSR={$BRSR}");
	}
	//删除
	public function delete() {
		$current_page = (isset($_GET['current_page']) && trim($_GET['current_page'])) ? intval($_GET['current_page']) : 0;
		$goods_id = (isset($_GET['goods_id']) && trim($_GET['goods_id'])) ? intval($_GET['goods_id']) : 0;
		$oldwhere = (isset($_GET['oldwhere']) && trim($_GET['oldwhere'])) ? trim($_GET['oldwhere']) : '';
		urldecode($oldwhere);
		if($current_page > 1){
			$current_page = ($current_page - 1)*10;
		}
		$sql = "DELETE FROM jf_goods WHERE id='{$goods_id}'";
		if($this->db->query($sql)){
			//删除该商品产品规格表
			$sql="DELETE FROM jf_goods_spec WHERE goods_id='{$goods_id}'";
			$this->db->query($sql);
			//删除该商品被添加的购物车
			$sql="DELETE FROM jf_cart WHERE goods_id='{$goods_id}'";
			$this->db->query($sql);
			//删除该商品的收藏
			$sql="DELETE FROM jf_favorite WHERE item_id='{$goods_id}' AND type_id = 1";
			$this->db->query($sql);
			
			echo"<meta charset='UTF-8'><script>alert('删除成功');window.location.href='?app=goods&act=index&BRSR={$current_page}&{$oldwhere}';</script>";
		}else{
			echo"<meta charset='UTF-8'><script>alert('删除失败');window.location.href='?app=goods&act=index&BRSR={$current_page}&{$oldwhere}';</script>";
		}
	}
	public function choose(){
        $where = '';
        $id = (isset($_GET['id'])) ? $_GET['id'] : '';
        $status = (isset($_GET['status'])) ? trim($_GET['status']) : '';
        $keyword = (isset($_GET['keyword']) && trim($_GET['keyword'])) ? trim($_GET['keyword']) : '';

        if ($keyword) {
            $where .= "AND (c.name LIKE '%{$keyword}%' OR c.price LIKE '%{$keyword}%' OR c.keywords LIKE '%{$keyword}%')";
        }
        if($id){
        	$where .="AND id = {$id}";
        }
        if($status){
        	$where .="AND status = {$status}";
        }
        //分页
        $this->setpages(array( 'keyword'=>$keyword,'id'=>$id,'status'=>$status));

        $sql = "SELECT c.*  FROM jf_goods c
			WHERE 1 {$where} ORDER BY c.id DESC";
        //echo $sql;
        $rs = $this->ezr->get_results($sql);
        $nav = $this->ezr->get_navigation();
        $this->smarty->assign('rs',$rs);
        $this->smarty->assign('keyword',$keyword);
        $this->smarty->assign('status',$status);
        $this->smarty->assign('page',$nav);
        $this->smarty->assign('id',$id);
        $this->display();
    }
    public function goods_qrcode(){
    	$where = '';
		//分页
		$id = (isset($_GET['id']) && trim($_GET['id'])) ? intval($_GET['id']) : '';
        $keyword = (isset($_GET['keyword']) && trim($_GET['keyword'])) ? trim($_GET['keyword']) : '';
        $shop_id = (isset($_GET['shop_id']) && trim($_GET['shop_id'])) ? intval($_GET['shop_id']) : 0;
        if ($keyword) {
            $where .= "AND (g.name LIKE '%{$keyword}%' OR g.huohao LIKE '%{$keyword}%' 
            	OR g.description LIKE '%{$keyword}%' OR g.content LIKE '%{$keyword}%')";
        }
        if ($id) {
            $where .= "AND g.id='{$id}'";
        }
        //分页
        $this->setpages(array('id'=>$id, 'keyword'=>$keyword ,'shop_id'=>$shop_id));
		$sql = "SELECT g.* FROM jf_goods g WHERE 1 {$where} ORDER BY g.id DESC";
		$rs = $this->ezr->get_results($sql);
		$nav = $this->ezr->get_navigation();
		if($rs){
		foreach ($rs as $key => $val) {
			$goods_id= $val['id'];
			$url = "{$this->domain}/wap.php?app=goods&act=detail&shop_id={$shop_id}&goods_id={$goods_id}&reseller=";
			$url = urlencode($url);
			$rs[$key]['url'] = $url;
		}
		}
		$sql = "SELECT id,name FROM jf_shop ORDER BY  id ASC";
		$shops = $this->db->get_results($sql);
		$this->smarty->assign('shops',$shops);
		$this->smarty->assign('rs',$rs);
		$this->smarty->assign('page',$nav);
		$this->smarty->assign('shop_id',$shop_id);
		$this->smarty->assign('where',$where);
    	$this->display();
    }
    //改变分类
    public function check_cate(){
    	$where = '';
    	$checkbox = (isset($_POST['checkbox']) && intval($_POST['checkbox'])) ? ($_POST['checkbox']) : '';
    	$catechange = (isset($_POST['catechange']) && trim($_POST['catechange'])) ? intval($_POST['catechange']) : 0;
    	if($checkbox !== '' && $catechange !== 0){
    		foreach ($checkbox as $key => $val) {
    			$sql = "UPDATE jf_goods SET category_id = '{$catechange}' WHERE id = {$val}";
    			$this->db->query($sql);
    		}
    	}
    	header("Location:?app=goods&act=index");
    }
    public function qrcode_out(){
    	set_time_limit(600);
		$where ='';
		$id = (isset($_GET['id']) && trim($_GET['id'])) ? intval($_GET['id']) : '';
        $keyword = (isset($_GET['keyword']) && trim($_GET['keyword'])) ? trim($_GET['keyword']) : '';
		$shop_id = (isset($_GET['shop_id']) && trim($_GET['shop_id'])) ? trim($_GET['shop_id']) : '';
		if ($keyword) {
            $where .= "AND (g.name LIKE '%{$keyword}%' OR g.huohao LIKE '%{$keyword}%' 
            	OR g.description LIKE '%{$keyword}%' OR g.content LIKE '%{$keyword}%')";
        }
        if ($id) {
            $where .= "AND g.id='{$id}'";
        }
		if($where || $shop_id){
		$sql = "SELECT name FROM jf_shop WHERE id = {$shop_id}";
		$shop_name = $this->db->get_row($sql);
		$sql = "SELECT g.id ,g.name FROM jf_goods g WHERE 1 {$where} ORDER BY g.id DESC";
		$row = $this->db->get_results($sql);
		//创建目录
		$path = ROOT_PATH.UPLOAD_PATH.'/qrcode/'.time().rand(000,999);
		mkdir($path,$mode = 0777);
		foreach ($row as $val) {
			$goods_id = $val->id;
			$url = "{$this->domain}/wap.php?app=goods&act=detail&shop_id={$shop_id}&goods_id={$goods_id}&reseller=";
			$url = urldecode($url);
			$goods_name=$val->name;
			$name= $shop_name->name.$goods_name;
			$name =$this->replace_specialChar($name);
			$name = iconv('UTF-8', 'GB2312//IGNORE', $name);
			$this->qr_out($url,$name,$path);

		}
		require_once (FRAMEWORK_PATH .'/third/phpqrcode/PHPZip.php');
		// $path =ROOT_PATH.UPLOAD_PATH.'/qrcode';
		$zip = new PHPZip();
		//压缩并下载并
		$zip->ZipAndDownload($path);
		//删除文件夹
		rmdir($path);
		}else{
			echo "<meta charset='UTF-8'><script>alert('请填入搜索条件！');</script>";
		}
	}
	public function qr_out($url,$name,$path){
		require_once (FRAMEWORK_PATH .'/third/phpqrcode/phpqrcode.php');
		return QRcode::pngg($url,$name,$path,$outfile=TRUE,$saveandprint=TRUE);
	}
    public function qrcode(){
		require_once (FRAMEWORK_PATH .'/third/phpqrcode/phpqrcode.php');
		$url = urldecode($_GET["url"]);
		return QRcode::png($url);
	}
	//删除字符串中的特殊字符
	public function replace_specialChar($strParam){
    	$regex = "/\/|\~|\!|\@|\#|\\$|\%|\^|\&|\*|\(|\)|\_|\+|\{|\}|\:|\<|\>|\?|\[|\]|\,|\.|\/|\;|\'|\`|\-|\=|\\\|\|\、|\、|\s|/";
    	return preg_replace($regex,"",$strParam);
	}
}
